﻿using JobSearcherAPI.Models;
using JobSearcherAPI.Search;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JobSearcherAPI
{
    internal class JobSite
    {
        public static List<string> SearchEngineList = new List<string>();
        public static List<JobSite> JobSites = new List<JobSite>();

        //public static readonly JobSite CareerBuilder = new JobSite("Career Builder", new CareerBuilderSearcher());
        public static readonly JobSite DigitalGov = new JobSite("DigitalGov", new DigitalGovSearcher());
        public static readonly JobSite GitHub = new JobSite("GitHub", new GitHubSearcher());
        public static readonly JobSite Indeed = new JobSite("Indeed", new IndeedSearcher());

        public static JobSite SearcherByName(string name)
        {
            foreach (JobSite js in JobSites)
            {
                if (js.ToString().Equals(name))
                    return js;
            }
            return null;
        }

        public static void AddKeys(string keyFileString)
        {
            var keys = JObject.Parse(keyFileString);
            Indeed.AddApiID((string)keys.SelectToken("Indeed").SelectToken("id"));
        }

        private string _name;
        private SearcherBase _searcher;

        private JobSite(string name, SearcherBase searcher)
        {
            _name = name;
            _searcher = searcher;

            SearchEngineList.Add(name);
            JobSites.Add(this);
        }

        private void AddApiID(string id)
        {
            _searcher.API_ID = id;
        }

        private void AddApiKey(string key)
        {
            _searcher.Key = key;
        }

        public override string ToString()
        {
            return _name;
        }

        public async Task<SearchResult> GetSearchResults(SearchArguments arguments)
        {
            var result = await _searcher.Search(arguments);
            return result;
        }
    }
}
